package com.time.workshop.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.time.workshop.API;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.adapter.AbsViewHolderAdapter;
import com.time.workshop.bean.BaseListResponse;
import com.time.workshop.bean.Order;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;
import com.time.workshop.view.RefreshViewLoadMoreProxy;
import com.time.workshop.view.RefreshViewLoadMoreProxy.OnLoadDataActionListener;

/**
 * @author fanbaolong
 * 
 *         订单列表
 */
public class OrderListActivity extends SuperActivity implements OnLoadDataActionListener
{

	@V
	private Button orderlist_titlebar_left_btn;
	@V
	private RadioGroup order_radioGroup;
	@V
	private RadioButton order_radio0, order_radio1, order_radio2;
	@V
	private RelativeLayout order_empty_relat;

	private OrderAdapter mAdapter;

	private class Response extends BaseListResponse<Order>
	{
	}

	private Response mResponse;

	private List<Order> mOrders = new ArrayList<Order>();

	private String status = "1,2,3";
	private int nowPage = 1;

	PullToRefreshListView mRefreshListView;
	RefreshViewLoadMoreProxy mLoadMoreProxy;

	@Override
	protected void onCreate(Bundle arg0)
	{
		super.onCreate(arg0);
		setContentView(R.layout.activity_orderlist);
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		init();
	}

	private void init()
	{
		Injector.getInstance().inject(this);

		order_radio0.setOnClickListener(this);
		order_radio1.setOnClickListener(this);
		order_radio2.setOnClickListener(this);

		orderlist_titlebar_left_btn.setOnClickListener(this);

		mRefreshListView = (PullToRefreshListView) findViewById(R.id.order_list);
		mRefreshListView.setEmptyView(order_empty_relat);
		mRefreshListView.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));

		mAdapter = new OrderAdapter(this, R.layout.item_order_list);

		mRefreshListView.setAdapter(mAdapter);

		mLoadMoreProxy = new RefreshViewLoadMoreProxy(mRefreshListView, Mode.BOTH, 0, 0);
		mLoadMoreProxy.setOnLoadDataActionListener(this);

		getData(getIntent().getStringExtra("status"));

		mRefreshListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				Intent intent = new Intent(OrderListActivity.this, OrderDetailActivity.class);
				intent.putExtra("orderId", mAdapter.getData().get(position - 1).getID());
				startActivity(intent, true);

			}

		});

	}

	private void initFinalData(String sta, int pag, final boolean isRefresh)
	{

		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("token", PreferenceUtil.getString(this, BaseConstant.PREF_TOKE_STRING));
		params.put("status", sta);
		params.put("page", pag + "");
		params.put("t", new Date().getTime() + "");

		fh.get(API.orderList, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onSuccess(Object result)
			{
				super.onSuccess(result);

				Gson gson = new Gson();
				mResponse = gson.fromJson(result.toString(), Response.class);

				if (mResponse.isSuccess())
				{

					mOrders = mResponse.getResult();

					Log.d("mOrders==", mOrders.toString());

					if (isRefresh)
						mAdapter.update(mOrders);
					else
						mAdapter.append(mOrders);

				} else
				{
					NewToast.makeText(getApplicationContext(), mResponse.getMessage());
				}
				// 因为后台没有返回页数，这里是为了区别第一页为空和加载更多时为空的情况
				if (mOrders.size() == 0 && mAdapter.getData().size() == 0)
				{
					NewToast.makeText(getApplicationContext(), "暂时没有数据");
				} else if (mOrders.size() == 0 && mAdapter.getData().size() != 0)
				{
					NewToast.makeText(getApplicationContext(), "已经是最后一页了");
				}
				mRefreshListView.onRefreshComplete();

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg)
			{
				super.onFailure(t, errorNo, strMsg);
				Log.d(TAG, "onFailure==" + strMsg);
				NewToast.makeText(getApplicationContext(), strMsg);
				mRefreshListView.onRefreshComplete();
			}
		});

	}

	public void onClick(View v)
	{

		switch (v.getId())
		{
		case R.id.orderlist_titlebar_left_btn:
			back();
			break;

		case R.id.order_radio0:
			getData("process");
			break;

		case R.id.order_radio1:
			getData("delivery");
			break;

		case R.id.order_radio2:
			getData("completed");
			break;

		default:
			break;
		}

	}

	private void getData(String status1)
	{
		if ("process".equals(status1))
		{
			order_radio0.setChecked(true);
			status = "1,2,3";
		} else if ("delivery".equals(status1))
		{
			order_radio1.setChecked(true);
			status = "4";
		} else if ("completed".equals(status1))
		{
			order_radio2.setChecked(true);
			status = "5,90";
		}
		nowPage = 1;
		initFinalData(status, nowPage, true);
		mRefreshListView.setSelection(0);
	}

	public class OrderAdapter extends AbsViewHolderAdapter<Order>
	{

		public OrderAdapter(Context context, int layoutRes)
		{
			super(context, layoutRes);
		}

		@Override
		protected void bindData(final int pos, final Order itemData)
		{

			TextView mOrderStatusText = getViewFromHolder(R.id.order_status_text);
			TextView mOrderOperationText = getViewFromHolder(R.id.order_operation_textbut);
			TextView mOrderStoreText = getViewFromHolder(R.id.order_store_text);
			TextView mOrderTimeText = getViewFromHolder(R.id.order_time_text);
			TextView mOrderPriceText = getViewFromHolder(R.id.order_price_text);

			mOrderStatusText.setText(itemData.getSTATUS_NAME());
			mOrderStoreText.setText(itemData.getTITLE());
			mOrderTimeText.setText(itemData.getC_TIME());

			if (itemData.getSTATUS() == 1)
			{
				// mOrderOperationText.setText("去付款");
				// mOrderOperationText.setVisibility(View.GONE);
				mOrderOperationText.setText(itemData.getSTATUS_NAME());
				mOrderOperationText.setVisibility(View.VISIBLE);
				mOrderOperationText.setBackgroundResource(R.drawable.btn_pay_normal);
				// mOrderOperationText.setBackgroundResource(R.drawable.btn_pay_pressed);
				mOrderPriceText.setText("待定");

			} else if (itemData.getSTATUS() == 2)
			{
				// mOrderOperationText.setVisibility(View.GONE);
				mOrderOperationText.setText(itemData.getSTATUS_NAME());
				mOrderOperationText.setVisibility(View.VISIBLE);
				mOrderOperationText.setBackgroundResource(R.drawable.btn_pay_normal);
				mOrderPriceText.setVisibility(View.VISIBLE);
				mOrderPriceText.setText("￥" + itemData.getPRICE());

			} else if (itemData.getSTATUS() == 3)
			{
				// mOrderOperationText.setVisibility(View.GONE);
				mOrderOperationText.setText("去付款");
				mOrderOperationText.setVisibility(View.VISIBLE);
				mOrderOperationText.setBackgroundResource(R.drawable.btn_pay_pressed);
				mOrderPriceText.setVisibility(View.VISIBLE);
				mOrderPriceText.setText("￥" + itemData.getPRICE());
			} else if (itemData.getSTATUS() == 4)
			{
				mOrderOperationText.setText("发货中");
				mOrderOperationText.setVisibility(View.INVISIBLE);
				mOrderPriceText.setVisibility(View.VISIBLE);
				mOrderPriceText.setText("￥" + itemData.getPRICE());
			} else if (itemData.getSTATUS() == 5)
			{
				mOrderOperationText.setText("已完成");
				mOrderOperationText.setVisibility(View.INVISIBLE);
				mOrderPriceText.setVisibility(View.VISIBLE);
				mOrderPriceText.setText("￥" + itemData.getPRICE());
			}
		}

	}

	@Override
	public void onRefresh(int page)
	{
		initFinalData(status, 1, true);
	}

	@Override
	public void onLoadMore(int page)
	{
		nowPage++;
		initFinalData(status, nowPage, false);
	}

}
